package com.shine.sys.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shine.common.repository.hibernate.HibernateDao;
import com.shine.common.service.BaseServiceImpl;
import com.shine.sys.entity.RoleResource;
import com.shine.sys.service.RoleResourceService;


@Service
public class RoleResourceServiceImpl extends BaseServiceImpl<RoleResource, Integer> implements RoleResourceService {
	
	@Autowired
	private HibernateDao<RoleResource, Integer> basedaoRoleResource ;
	
	@Override
	public RoleResource save(RoleResource entity) {
		//先删除原有的角色资源权限
		deleteByRoleId(entity.getRoleId());
		
		if(null != entity.getResourceIds() && !"".equals(entity.getResourceIds().trim())) {
			return super.save(entity);
		}
		return entity ;
	}
	
	private void deleteByRoleId(Integer roleId) {
		this.basedaoRoleResource.executeSQL("delete from sys_role_resource where roleId=?", new Object[]{roleId}) ;
	}

	@Override
	public RoleResource getOneByRoleId(Integer roleId) {
		String queryString = "select t.* from sys_role_resource t where t.roleId=?" ;
		return (RoleResource) this.basedaoRoleResource.findUniqueSQL(queryString, new Object[]{roleId}, RoleResource.class, true) ;
	}
	
}
